<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %><%--
  Created by IntelliJ IDEA.
  User: 86155
  Date: 2021/10/14
  Time: 13:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");%>
<%String uname = request.getParameter("uname");
    String pword = request.getParameter("pword");
    if(session.getAttribute("uname") == null){//用户登录初始化
        session.setAttribute("uname",uname);

        //更新在线人数
        if(application.getAttribute("numOfPeop") == null){
            application.setAttribute("numOfPeop",1);
        }else{
            application.setAttribute("numOfPeop",(Integer)application.getAttribute("numOfPeop")+1);
        }
        //更新在线人员名单
        if(application.getAttribute("peopList") == null){
            ArrayList<String> peopList = new ArrayList<>();
            peopList.add(uname);
            application.setAttribute("peopList",peopList);
        }else{
            ArrayList<String> peopList = (ArrayList<String>) application.getAttribute("peopList");
            peopList.add(uname);
            application.setAttribute("peopList",peopList);
        }
    }%>
<%String[] info3 = new String[0];
    if(application.getAttribute("chatInfo") != null){
        StringBuffer info1 = (StringBuffer) application.getAttribute("chatInfo");
        String info2 = info1.toString();
        info3 = info2.split("\\|");
    }%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>聊天室</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="./static/css/chat.css">
    <script type="text/javascript" src="./jquery/jquery-3.5.1.min.js"></script>
    <script type="text/javascript" src="./static/js/listen.js" charset="utf-8"></script>
</head>

<body>
<div class="container">
    <!-- 头信息 -->
    <div class="row head">
        <div class="col-md-4 head-item text-center" >Bug-Chat</div>
        <div class="col-md-4 head-item text-center"  >在线人数：<span id="numOfPeop"><%=application.getAttribute("numOfPeop")%></span></div>
        <div class="col-md-4 head-item text-center" >登入用户：<%=session.getAttribute("uname")%></div>
    </div>
    <!-- body -->
    <div class="row content">
        <!-- left -->
        <div class="col-md-8 left-content">
            <div class="content-chat">
                <table class="content-table" id="tid">
                </table>
            </div>
            <form action="" id="form1" onsubmit="return false;" method="post" class="left-form">
                <!-- 聊天内容输入 -->
                <textarea class="form-control" id="chatcon" name="chatInfo" rows="3"></textarea>
                <!-- 发送按钮 -->
                <input class="btn btn-default" onclick="send()" type="submit" value="发送">
            </form>
            <form action="exit.jsp" method="post">
                <!-- 退出按钮 -->
                <input class="btn btn-default" type="submit" value="退出">
            </form>
        </div>
        <!-- right -->
        <div class="col-md-4 right-content">
            <!-- 头像 -->
            <!-- 显示在线人数 -->
            <table class="table table-hover right-table" id="ppl">
                <tr><th>在线用户</th></tr>
                <%if(application.getAttribute("peopList") != null){
                    ArrayList<String> peopList = (ArrayList<String>) application.getAttribute("peopList");
                    Iterator<String> iterator = peopList.iterator();
                    while(iterator.hasNext()){%>
                        <%="<tr class=\"ppp\"><td>"+iterator.next()+"</td></tr>"%>
                    <%}
                }%>
            </table>
        </div>
    </div>
</div>
</body>
</html>
